<script type="text/javascript" src="js/storage.js"></script>
<script type="text/javascript" src="js/popup.js"></script>
<script>
  
  /* holds array of links received from content-script*/
  links = null;
  
  // *********** LISTENERS **************
  
  /*
   * this listener gets content_script results when user changed active tab
   */
  chrome.tabs.onSelectionChanged.addListener(function(tabId, info){
  
    updateLinks(tabId);
    
  });
  
   /*
    *  this listener gets content_script results when user created a new tab
	*/
  chrome.tabs.onCreated.addListener(function(tab){
    updateLinks(tab.id);
  });
  
   /*
    * this listener gets content_script results when tab is updated
	*/
  chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
    updateLinks(tabId);
  });
 
   /* this listener is called by the content-script "match_content_script.js" and updates
    * the badge and triggers a list refresh.
    */
  chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
    console.log(request);
    //alert(links);
    if (request == null) {
      chrome.browserAction.setBadgeText({});
      updateList();
    }
    else {
      links = request;
      if (links.length == 0) {
        chrome.browserAction.setBadgeText({
          text: ""
        });
      }
      else {
        chrome.browserAction.setBadgeText({
          text: links.length.toString()
        });
      }
      chrome.browserAction.setBadgeBackgroundColor({
        color: [0, 0, 164, 255]
      });
      updateList();
      
    }
    sendResponse({});
  });
  
  // *********** LOCAL STORAGE **************
  
  /** directly store a file into local storage.
    * href : url
    * text : anchor text
	* return : none
	*/
  function addToLibrary(href, text){
	chromeRadio.storage.storeNewFile(href,text);
  }
  
  /* check for popup.html if given mp3 already is in local storage.
   * href : url
   * return : true or false
   */
  function mp3IsInLibrary(href){
    console.log("checking: " + href);
    //if (chromeRadio.storage.getItem(chromeRadio.storage.urlPrefix + href) != null) {
	if(chromeRadio.storage.getURL(href) != null)
	{
      return true;
    }
    else {
      return false;
    }
  }
  
  // ************************
  
  /**
   * sends request to update the list.
   */
  function updateLinks(tabId){
    chrome.tabs.sendRequest(tabId, {
      update: "true"
    });
  }
  
  
  /* play file instantly from popup.html
   * href : url of file
   * return : none
   */
  function playFile(href){
  
    // first check if the library is already open
    // if yes set myLibraryTabId (defined at top of this file) 
    // just so we know that we can send it the play-message.
    chrome.tabs.getAllInWindow(null, function(tabs){
    
      create = true;
      for (i = 0; i < tabs.length; i++) {
        if (tabs[i].title.indexOf("My chromeRadio Library") >= 0) {
          create = false;
        }
      }
      
      if (create) {
        // library not open. open it, then send request.
        // note: it only seems to work if "selected: true" (default) 
        // (meaning: it has to be active for some reason.)
        chrome.tabs.create({
          url: "my-library.html"
        });
      }
      
    });
    
    // this seems neccessary....
    window.setTimeout("sendDelayedPlayRequest('" + href + "')", 750);
  }
  
  /**
   * actual function that sends the request to storage.js to play the file.
   * it seems neccessary to have a delay for stability and guaranteed functionality.
   * href : url
   * return : none
   */
  function sendDelayedPlayRequest(href){
    chrome.extension.sendRequest({
      playme: href
    });
  }
  
  
  
</script>

